package com.data.modules.gd.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.common.util.ConvertUtils;
import com.data.common.util.MyDateUtils;
import com.data.modules.gd.entity.GdLiveChannel;
import com.data.modules.gd.entity.GdProgramme;
import com.data.modules.gd.service.IGdLiveChannelService;
import com.data.modules.gd.service.IGdProgrammeService;
import com.data.modules.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 广电节目单
 * @Author: data-boot
 * @Date:   2022-11-29
 * @Version: V1.0
 */
@Api(tags="广电节目单")
@RestController
@RequestMapping("/gd/gdProgramme")
@Slf4j
public class GdProgrammeController extends BootController<GdProgramme, IGdProgrammeService> {
	@Autowired
	private IGdProgrammeService gdProgrammeService;

	@Autowired
	private IGdLiveChannelService gdLiveChannelService;

	/**
	 * 分页列表查询
	 *
	 * @param gdProgramme
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "广电节目单-分页列表查询")
	@ApiOperation(value="广电节目单-分页列表查询", notes="广电节目单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GdProgramme gdProgramme,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GdProgramme> queryWrapper = QueryGenerator.initQueryWrapper(gdProgramme, req.getParameterMap());
		Page<GdProgramme> page = new Page<GdProgramme>(pageNo, pageSize);

		IPage<GdProgramme> pageList = gdProgrammeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "获取广电回看节目单")
	 @ApiOperation(value="获取广电回看节目单列表查询", notes="获取广电回看节目单列表查询")
	 @PostMapping(value = "/querySchedulelist")
	 public Result<?> querySchedulelist(GdProgramme gdProgramme,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		 LambdaQueryWrapper<GdProgramme> wrapper = new LambdaQueryWrapper();
		 wrapper.eq(ObjectUtil.isNotEmpty(gdProgramme.getIsLabel()),GdProgramme::getIsLabel,gdProgramme.getIsLabel());
		 wrapper.eq(ObjectUtil.isNotEmpty(gdProgramme.getChannelId()),GdProgramme::getChannelId,gdProgramme.getChannelId());
		 wrapper.eq(ObjectUtil.isNotEmpty(gdProgramme.getCategoryName()),GdProgramme::getCategoryName,gdProgramme.getCategoryName());
		 wrapper.le(GdProgramme::getStartDate, MyDateUtils.formatDateStr(new Date()));

		 List<GdProgramme> list = gdProgrammeService.list(wrapper);

		 return Result.OK(list);
	 }

	@ApiOperation(value="按照频道分类查询广电频道表", notes="广电频道表-按照频道分类查询广电频道表")
	@PostMapping(value = "/queryChannelByCategoryName")
	public Result<?> queryChannelByCategoryName(GdLiveChannel gdLiveChannel) {

		List<GdLiveChannel> list= gdLiveChannelService.lambdaQuery().eq(GdLiveChannel::getCategoryName,gdLiveChannel.getCategoryName()).list();

		return Result.OK(list);
	}

	/**
	 *   批量操作打标签
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/lableBatch", method = RequestMethod.PUT)
	public Result<?> lableBatch(@RequestBody JSONObject jsonObject) {
		Result<?> result = new Result<SysUser>();
		try {
			String ids = jsonObject.getString("ids");
			String status = jsonObject.getString("status");
			String[] arr = ids.split(",");
			for (String id : arr) {
				if(ConvertUtils.isNotEmpty(id)) {

					this.gdProgrammeService.update(new GdProgramme().setIsLabel(1).setType(Integer.parseInt(status)),
							new UpdateWrapper<GdProgramme>().lambda().eq(GdProgramme::getId,id));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			result.error500("操作失败"+e.getMessage());
		}
		result.success("操作成功!");
		return result;

	}

	/**
	 *   添加
	 *
	 * @param gdProgramme
	 * @return
	 */
	@ApiOperation(value="广电节目单-添加", notes="广电节目单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GdProgramme gdProgramme) {
		gdProgrammeService.save(gdProgramme);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param gdProgramme
	 * @return
	 */
	@AutoLog(value = "广电节目单-编辑")
	@ApiOperation(value="广电节目单-编辑", notes="广电节目单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GdProgramme gdProgramme) {

		if(ObjectUtil.isEmpty(gdProgramme.getType())){
			gdProgramme.setIsLabel(0);
		}else{
			gdProgramme.setIsLabel(1);
		}

		gdProgrammeService.updateById(gdProgramme);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "广电节目单-通过id删除")
	@ApiOperation(value="广电节目单-通过id删除", notes="广电节目单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		gdProgrammeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "广电节目单-批量删除")
	@ApiOperation(value="广电节目单-批量删除", notes="广电节目单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.gdProgrammeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="广电节目单-通过id查询", notes="广电节目单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GdProgramme gdProgramme = gdProgrammeService.getById(id);
		if(gdProgramme==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(gdProgramme);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param gdProgramme
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GdProgramme gdProgramme) {
        return super.exportXls(request, gdProgramme, GdProgramme.class, "广电节目单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GdProgramme.class);
    }

}
